package com.cyandpos.pad.refund.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.RefundActivity;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RefundGoodsAdapter extends RecyclerView.Adapter<RefundGoodsAdapter.GoodsHolderr>  {

    private LayoutInflater mLayoutInflater;
    private ArrayList<Goods> mDatas;
    private RefundActivity mContext;
    private boolean isOrder;
    public RefundGoodsAdapter(Context context, ArrayList<Goods> goodsList, boolean isOrder) {
        mContext = (RefundActivity) context;
        this.mDatas = goodsList;
        this.isOrder = isOrder;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public GoodsHolderr onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GoodsHolderr(mLayoutInflater.inflate(R.layout.list_refund_item, parent, false));
    }

    @Override
    public void onBindViewHolder(GoodsHolderr holder,final int position) {
        int ticktGoodsInx =1;
        int ticktInx =1;
        if(mDatas.get(position).getTicktGoodsInx()!=null && mDatas.get(position).getTicktInx()!=null){
            ticktGoodsInx = Integer.parseInt(mDatas.get(position).getTicktGoodsInx());
            ticktInx = Integer.parseInt(mDatas.get(position).getTicktInx());
        }

        holder.mGood_xh.setText((ticktGoodsInx+1)+"/"+(ticktInx+1));
        holder.mGoodCode.setText(mDatas.get(position).getBarCode());
        holder.mGoodName.setText(mDatas.get(position).getName() + "(" + mDatas.get(position).getCode() + ")");
        holder.mGoodCount.setText(mDatas.get(position).getRefundCount());
//        holder.mGoodCount.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(mDatas.get(position).getSaleCount())-MoneyUtils.getMoneyInt(mDatas.get(position).getHasedRefundCount())));
        holder.mGoodPrice.setText(mDatas.get(position).getPrice());
        holder.mYH.setText(mDatas.get(position).getDiscount());
//        holder.mRefundCount.setText(mDatas.get(position).getRefundCount() + "");

        //折扣分摊
        double ZKFT = Double.valueOf(mDatas.get(position).getRefundCount())/Double.valueOf(mDatas.get(position).getSaleCount());
        holder.mMoney.setText(MoneyUtils.getMoneyStr((int)(MoneyUtils.getMoneyInt(mDatas.get(position).getRefundCount())*MoneyUtils.getMoneyInt(mDatas.get(position).getPrice())/100-MoneyUtils.getMoneyInt(mDatas.get(position).getDiscount())*ZKFT)));
        if(isOrder){
            ToastUtils.showMsg("整单退货不允许修改数量");
        }else {
            holder.mIvLeftSj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Goods goodListBean = mDatas.get(position);
                    if(Double.valueOf(goodListBean.getRefundCount()) == 0.0) return;
                    goodListBean.setRefundCount((Integer.valueOf(goodListBean.getRefundCount())-1)+"");
                    notifyDataSetChanged();
                    mContext.upDateShowRefund(mDatas);
                }
            });
            holder.mIvRightSj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Goods goodListBean = mDatas.get(position);
                    String strCount=String.valueOf(Integer.valueOf(goodListBean.getRefundCount())+Integer.valueOf(goodListBean.getHasedRefundCount()));
                    if(goodListBean.getSaleCount().equals(strCount)){
                        return;
                    }
                    goodListBean.setRefundCount((Integer.valueOf(goodListBean.getRefundCount())+1)+"");
                    notifyDataSetChanged();
                    mContext.upDateShowRefund(mDatas);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class GoodsHolderr extends RecyclerView.ViewHolder {
        @BindView(R.id.good_xh)
        TextView mGood_xh;
        @BindView(R.id.good_name)
        TextView mGoodName;
        @BindView(R.id.tv_sale_good_code)
        TextView mGoodCode;
        @BindView(R.id.good_count)
        TextView mGoodCount;
        @BindView(R.id.good_price)
        TextView mGoodPrice;
        @BindView(R.id.YH)
        TextView mYH;
        @BindView(R.id.money)
        TextView mMoney;
        @BindView(R.id.img_sale_goods_cut)
        ImageView mIvLeftSj;
//        @BindView(R.id.refund_count)
//        TextView mRefundCount;
        @BindView(R.id.img_sale_goods_add)
        ImageView mIvRightSj;
//        @BindView(R.id.discount)
//        TextView mDiscount;


        public GoodsHolderr(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
